﻿@{ ViewBag.Title = "Add";
    Layout = "~/Areas/Shared/_LayuiForm.cshtml"; }

<input style="display:none" id="classid" value="@ViewBag.ClassID">
<form class="layui-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">分类编码</label>
        <div class="layui-input-block layui-col-space3">
            <div class="layui-col-xs8 layui-col-sm8 layui-col-md8">
                <input type="text" name="SortID" id="SortID" placeholder="分类编码" autocomplete="off" class="layui-input" lay-verify="required|id" maxlength="20">
            </div>
            <div class="layui-col-xs4 layui-col-sm4 layui-col-md4">
                <select name="AutoType" id="AutoType" lay-filter="selectfilter">
                    <option value="">⇓自动生成编码</option>
                    <option value="英文">英文</option>
                    <option value="全拼">全拼</option>
                    <option value="首拼">首拼</option>
                </select>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">分类名称</label>
        <div class="layui-input-block">
            <input type="text" name="SortName" id="SortName" placeholder="分类名称" autocomplete="off" class="layui-input" lay-verify="required" maxlength="20">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">序 号</label>
        <div class="layui-input-block">
            <input type="text" name="SortOrder" placeholder="序号" autocomplete="off" class="layui-input" lay-verify="required|number" maxlength="2">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="add">新 建</button>
        </div>
    </div>
</form>
<script>
    layui.use(["form", "exLayer", "exUtils"], function () {
        let form = layui.form;
        let exLayer = layui.exLayer;
        let exUtils = layui.exUtils;

        let $ = layui.$;

        form.render(); //

        //放置顺序和位置有讲究
        form.verify({
            id: function (value, item) {
                if (!new RegExp("^[A-Za-z0-9]+$").test(value)) {
                    return '编码由英文字符和数字组成，不得包含中文、空格及其他字符';
                }
            }
        });

        form.verify({
            name: function (value, item) {
                if (!new RegExp("^[\u4e00-\u9fa5_a-zA-Z0-9]+$").test(value)) {
                    return '名称由汉字、数字、字母、下划线组成，不得包含其他字符';
                }
            }
        });

        form.on("submit(add)", function (data) {
            exUtils.ajax("/SysBasic/Sort/Add?classid=" + $("#classid").val(), "post", data.field, true).done(function (response) {
                exLayer.greenTickMsg(response.message, function () {
                    parent.layui.table.reload('tableId', { url: "/SysBasic/Sort/GetList?classid=" + $(window.parent.document).find("#classid").val() });
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                });
            }).fail(function (error) {
                console.log(error);
            });
            return false;
        });

        //自动生成编码
        form.on('select(selectfilter)', function (data) {
            var name = $("#SortName").val().trim();
            var type = data.value;
            if (name == "") {
                exLayer.yellowSighMsg("请输入名称");
                return false;
            }
            exUtils.ajax("/SysBasic/AutoCode/GetCode", "post", { name: name, type: type }, true).done(function (response) {
                $("#SortID").val(response.extra);
            }).fail(function (error) {
                console.log(error);
            });
            return false;
        });
    });
</script>